//
//  FStarTrendViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/5/15.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FStarTrendViewController.h"
#import "FansTrendDetailViewController.h"
#import "StarTrendTopView.h"
#import "FansTrdModel.h"
#import "FansTrdFrameModel.h"
#import "FanstrdNormalCell.h"

@interface FStarTrendViewController ()<UITableViewDelegate,UITableViewDataSource,FanstrdNormalCellDelegate>{
    NSMutableArray *_trendDataArray;
    NSMutableArray *_dataFrameArray;
}
@property (nonatomic, strong) UITableView *trendTableView;
@end

@implementation FStarTrendViewController
#pragma mark --  动态列表发送请求
- (void)FTrendListRequest {
    NSDictionary *dict;
    if (self.suid) {
        dict = @{
                 @"udid":User_ID,
                 @"token":TOKEN,
                 @"suid":self.suid,
                 @"page":@"1"
                 };
    } else {
        dict = @{
                 @"udid":User_ID,
                 @"token":TOKEN,
                 @"page":@"1"
                 };
    }
    
    NSString *urlString = [TEST_URL stringByAppendingString:kFTrendList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            if (_dataFrameArray.count > 0) {
                [_dataFrameArray removeAllObjects];
            }
            for (NSDictionary *trendDict in array) {
                FansTrdModel *model = [[FansTrdModel alloc] initWithDict:trendDict];
                NSArray *imageArray = [model.c_images componentsSeparatedByString:@","];
                model.pictureArray = [NSMutableArray arrayWithArray:imageArray];
                [_trendDataArray addObject:model];
                FansTrdFrameModel *frameModel = [[FansTrdFrameModel alloc] init];
                frameModel.trendModel = model;
                [_dataFrameArray addObject:frameModel];
            }
            [self.trendTableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self FTrendListRequest];
    [self setUpUI];
    [self setNav];
}
- (void)setUpUI {
    _dataFrameArray = [NSMutableArray arrayWithCapacity:1];
    _trendDataArray = [NSMutableArray arrayWithCapacity:1];
    [self creatTableView];
}
- (void)setNav {
    self.navigationItem.title = @"动态";
}

- (void)creatTableView {self.trendTableView = [[UITableView alloc] init];
    self.trendTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kNAV_HEIGHT)];
    
    self.trendTableView.delegate = self;
    self.trendTableView.dataSource = self;
    self.trendTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.trendTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.trendTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.trendTableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataFrameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FanstrdNormalCell *cell = [FanstrdNormalCell loadNormalCellWithTabelView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    cell.frameModel = model;
    cell.delegate = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FansTrendDetailViewController *detailVC = [FansTrendDetailViewController new];
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    detailVC.trendFrameModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FansTrdFrameModel *model = _dataFrameArray[indexPath.row];
    return model.cellHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
